{}
¶meals = {
'breakfast': 'oatmeal',
'lunch': 'leftovers',
'dinner': 'spaghetti'
}
type(meals)
dict
meals
{'breakfast': 'oatmeal', 'lunch': 'leftovers', 'dinner': 'spaghetti'}
meals['breakfast']
'oatmeal'
meals['lunch']
'leftovers'
meals['leftovers']
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) Input In [7], in <cell line: 1>() ----> 1 meals['leftovers'] KeyError: 'leftovers'
meals = {
'breakfast': 'candy',
'lunch': 'pizza',
'breakfast': 'oatmeal',
'dinner': 'sandwiches'
}
meals
{'breakfast': 'oatmeal', 'lunch': 'pizza', 'dinner': 'sandwiches'}
for meal, offering in meals.items():
print(f'For {meal} you get {offering}. 😋')
For breakfast you get oatmeal. 😋 For lunch you get pizza. 😋 For dinner you get sandwiches. 😋
for meal in meals: # iterate over keys
print(meal)
breakfast lunch dinner
for dish in meals.values(): # iterate over values
print(dish)
oatmeal pizza sandwiches
list(meals.items())
[('breakfast', 'oatmeal'), ('lunch', 'pizza'), ('dinner', 'sandwiches')]
in
+ dict
¶hometowns = {
'Dallin Oaks': 'Provo, UT',
'Jeffery Holland': 'St George, UT',
'Merril Bateman': 'Lehi, UT',
'Cecil Samuelson': 'Salt Lake City, UT',
'Kevin Worthen': 'Dragerton, UT',
'Shane Reese': 'Logan, UT'
}
'Shane Reese' in hometowns
True
'Karl Maeser' in hometowns
False
for person in ['Kevin Worthen', 'Henry Eyring', 'Shane Reese', 'Ernest Wilkinson', 'Karl Maeser']:
if person in hometowns:
print(f'{person} was born in {hometowns[person]}.')
else:
print(f"I don't know where {person} was born.")
Kevin Worthen was born in Dragerton, UT. I don't know where Henry Eyring was born. Shane Reese was born in Logan, UT. I don't know where Ernest Wilkinson was born. I don't know where Karl Maeser was born.
You are given a dictionary mapping words to emojis.
Replace all instances of a word with it's corresponding emoji. Ignore case.
So, given a dictionary
emojis = {'dog': '🐶', 'cat': '🐱', 'tree': '🌳', 'bird': '🐦'}
The phrase
My dog has fleas.
Becomes
My 🐶 has fleas.
Community members want to register for the Rec Center sports teams.
You have a dictionary that maps age groups to team names.
Map a list of tuples containing a name and age to a list of tuples containing name and team.
To compute a participants age group, find the nearest multiple of 3 that is less than or equal to the participant age.
If the participant does not have an age-group assignment, they go in team "Old Fogies".
Given a dictionary mapping letters to other letters (called a "codex"), encode a message.
The codex only contains lower-case letters, but you should encode upper-case letters also. Preserve casing.
dict
{}
key in dict
[]